package cn.edu.jcu.kyle.gintama;

import cn.edu.jcu.kyle.gintama.OurUtil.AlertWindow;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DbUtil {
    private static final String mysqlDriver = "com.mysql.cj.jdbc.Driver";
    private static String userName = "root";    //方便调试，这个地方可以先写死成自己的数据库用户名和密码
    private static String userPassword = "root";//同上

    public static boolean dbInit(String _userName, String _userPassword) {//数据库初始化，没有gintama数据库则创建
        userName = _userName;                                             //你们自己的要用的表单去自己的方法里创建
        userPassword = _userPassword;
        try {
            Connection coon = getMySQLConnection("jdbc:mysql://localhost:3306/?useUnicode=utf8&useSSL=false");
            if (coon == null) {
                return false;
            }
            DbUtil.executeUpdate(coon, "create database if not exists gintama");
            coon.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            try {
                AlertWindow.getWindow("数据库初始化失败！请重试");
            } catch (Exception r) {
                r.printStackTrace();
                return false;
            }
            return false;
        }
    }

    public static Connection getConnection(String driver, String url) {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, userName, userPassword);
        } catch (Exception e) {
            AlertWindow.getWindow("数据库连接失败\n请确认用户名和密码是否正确！");
            e.printStackTrace();
        }
        return conn;
    }

    public static Connection getMySQLConnection(String url) {
        return getConnection(mysqlDriver, url);
    }

    public static void closeAll(Connection conn, PreparedStatement preparedStatement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void closeAll(Connection conn, ResultSet resultSet) {
        closeAll(conn, null, resultSet);
    }

    public static void closeAll(Connection conn, PreparedStatement preparedStatement) {
        closeAll(conn, preparedStatement, null);
    }

    public static int executeUpdate(Connection conn, String sql, Object[] params) {
        int result = 0;
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    preparedStatement.setObject(i + 1, params[i]);
                }
            }
            result = preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, preparedStatement);
        }
        return result;
    }

    public static int executeUpdate(Connection conn, String sql) {
        return executeUpdate(conn, sql, null);
    }

    public static ResultSet executeQuery(Connection conn, String sql, Object[] params) {
        ResultSet result = null;
        PreparedStatement preparedStatement;
        try {
            preparedStatement = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    preparedStatement.setObject(i + 1, params[i]);
                }
            }
            result = preparedStatement.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static ResultSet executeQuery(Connection conn, String sql) {
        return executeQuery(conn, sql, null);
    }
}
